//
//  MyselfViewController.m
//  MIDI
//
//  Created by 邵子凡 on 15/11/22.
//  Copyright © 2015年 QXSX. All rights reserved.
//

#import "MyselfViewController.h"
#import "AddHeadImageCell.h"
#import "AddExamCell.h"
#import "AppConfig.h"
#import "AddLineViewCell.h"
#import "UserDataSingleton.h"
#import "UIViewController+Create.h"
#import "MyTestViewController.h"
#import "MyOrderViewController.h"
#import "MyMessageViewController.h"
#import "AboutMIDIViewController.h"
#import "ShowApplyViewController.h"
#import "SystemSetViewController.h"
#import "PersonalViewController.h"
#import "SelfInfoModel.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "SelfInfoModel.h"
#import "UIImageView+WebCache.h"
#import "CommFunc.h"
#import "MBProgressHUD+YC.h"
#import "GradingTestSongAudioViewController.h"
#import "AccountTool.h"


@interface MyselfViewController ()<UITableViewDelegate,UITableViewDataSource,AddHeadImageCellDelegate, QXNetWorkToolDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation MyselfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
//    设置tableView样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置tableView背景颜色
    self.tableView.backgroundColor = UIColorRGBA(242, 243, 246, 1);
    
    //设置导航标题
    self.navigationItem.title = @"我的";
    
    //设置导航栏标题字体颜色（字体大小也可以在这里设置）
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    [self useMethodToFindBlackLineAndHind];
    [self.tableView reloadData];
    [self reqSelfInfo];
}

-(void) configNavigationItem
{
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.tabBarController.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    UIButton *barButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 15)];
    
    [barButton setImage:[UIImage imageNamed:@"btn_navigation_back"] forState:UIControlStateNormal];
    [barButton setImage:[UIImage imageNamed:@"btn_navigation_back"] forState:UIControlStateNormal];
    [barButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
    [barButton addTarget:self action:@selector(pressLeft:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:barItem];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
    {
        return [CommFunc getMyselfHeadCellHeight];
                
    }else if (indexPath.row == 1){
        
        return 50;
    }else if (indexPath.row == 2){
        
        return 50;
    }else if (indexPath.row == 3){
        
        return 50;
    }else if (indexPath.row == 4){
        
        return 10;
    }else if (indexPath.row == 5){
        
        return 50;
    }else if (indexPath.row == 6){
        
        return 50;
    }else if (indexPath.row == 7){
        
        return 10;
    }else if (indexPath.row == 8){
        
        return 50;
    }else if (indexPath.row == 9){
        
        return 50;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        
        AddHeadImageCell *cell = [AddHeadImageCell cellWithTableView:tableView];
        
        cell.delegete = self;
                
        return cell;
    }else if (indexPath.row == 1){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"我的考试";
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_myexam"];
        
        return cell;
        
    }else if (indexPath.row == 2){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"我的订单";
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_myorder"];
        
        return cell;
    }else if (indexPath.row == 3){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"我的消息";
        
        return cell;
    }else if (indexPath.row == 4){
        
        AddLineViewCell *cell = [AddLineViewCell cellWithTableView:tableView];
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_mymessage"];
        
        return cell;
    }
    else if (indexPath.row == 5){
    
    AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
    
    cell.label.text = @"考级音频（已下载）";
    
    //        cell.image.image = [UIImage imageNamed:@"ico_myselfview_aboutmidi"];
    
    return cell;
    }
    else if (indexPath.row == 6){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"关于迷笛";
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_aboutmidi"];
        
        return cell;
    }else if (indexPath.row == 7){
        
        AddLineViewCell *cell = [AddLineViewCell cellWithTableView:tableView];
        
        return cell;
    }else if (indexPath.row == 8){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"申请展示";
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_applyshow"];
        
        return cell;
    }else if (indexPath.row == 9){
        
        AddExamCell *cell = [AddExamCell cellWithTableView:tableView];
        
        cell.label.text = @"系统设置";
        
//        cell.image.image = [UIImage imageNamed:@"ico_myselfview_setting"];
        
        return cell;
    }
    
    return nil;
}

//cell点击事件push
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 1) {
        
        UserDataSingleton *userData = [UserDataSingleton shareInstance];
        if(![userData isLogin])
        {
            [userData pleaseLogin:self];
            return;
        }
        
        MyTestViewController *obj = [MyTestViewController create];
        obj.isReturnTOShouYe = NO;
        [self.navigationController pushViewController:obj animated:YES];
        
    }else if (indexPath.row == 2){
        
        UserDataSingleton *userData = [UserDataSingleton shareInstance];
        if(![userData isLogin])
        {
            [userData pleaseLogin:self];
            return;
        }
        
        [self.navigationController pushViewController:
         [MyOrderViewController create] animated:YES];
        
    }else if (indexPath.row == 3){
        
        UserDataSingleton *userData = [UserDataSingleton shareInstance];
        if(![userData isLogin])
        {
            [userData pleaseLogin:self];
            return;
        }
        
        [self.navigationController pushViewController:
         [MyMessageViewController create] animated:YES];
        
    }
    else if (indexPath.row == 5){
        
        //下载列表
        
        GradingTestSongAudioViewController * vc = [GradingTestSongAudioViewController create];
        
        vc.isFromDownLoad = YES;
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    else if (indexPath.row == 6){
        
        [self.navigationController pushViewController:
         [AboutMIDIViewController create] animated:YES];
        
    }else if (indexPath.row == 8){
        
        [self.navigationController pushViewController:
         [ShowApplyViewController create] animated:YES];
        
    }else if (indexPath.row == 9){
        
        [self.navigationController pushViewController:
         [SystemSetViewController create] animated:YES];
        
    }
}


- (void)uploadHeadImageBtn{
    
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if(![userdata isLogin])
    {
        [userdata pleaseLogin:self];
        return;
    }

    [self.navigationController pushViewController:    [PersonalViewController create] animated:YES];
}

#pragma mark --- QxnetTool-------------------
-(void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    [MBProgressHUD showError:(NSString *)Response toView:self.view];
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if(tag == 1)
    {
        SelfInfoModel * data = nil;
        for(NSDictionary * dic in Response)
        {
            data = [SelfInfoModel MyModelWithDict:dic];
            break;
        }
        if(data == nil)
            return;
        UserDataSingleton *userdata = [UserDataSingleton shareInstance];
        userdata.isGotSelfInfo = YES;
        userdata.midi_id = data.midi_id;
        userdata.sex = data.sex;
        userdata.eamil = data.eamil;
        userdata.people = data.people;
        userdata.nationality = data.nationality;
        userdata.id_card = data.id_card;
        userdata.head_img = data.head_img;
        userdata.nickname = data.nickname;
        userdata.real_name = data.real_name;
        userdata.phone = data.phone;
        userdata.birthday = data.birthday;
        userdata.role_id = data.role_id;
        userdata.ismidisir = data.ismidisir;
        userdata.level = data.level;
        
        [AccountTool saveNickName:userdata.nickname];
        [AccountTool saveMidiID:userdata.midi_id];
        
        if (userdata.majorList)
        {
            [userdata.majorList removeAllObjects];
        }
        
        if (userdata.majorList == nil)
        {
            userdata.majorList = [[NSMutableArray alloc] init];
        }
        
        for (int i = 0; i < [data.majorList count]; ++i)
        {
            MajorListOneModel *one =[MajorListOneModel MyModelWithDict:data.majorList[i]];
            
            [userdata.majorList addObject:one];
        }
        
        [self.tableView reloadData];
    }
}

//请求个人数据
-(void)reqSelfInfo
{
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if(![userdata isLogin])
        return;
//    if(userdata.isGotSelfInfo && !userdata.isSelfInfoChange)
//        return;
    
    //没有网络
    if (![CommFunc isConnectionAvailable])
    {
        return;
    }
    
    [MBProgressHUD hideHUDForView:self.view];
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
    
    tool.QXNetWorkDelegate = self;
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:GetSelfInfoInterface andParameter:@[@{@"user_id":userdata.user_id}] tag:1];
}

@end




















